package com.gensdai.leliang.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.parseBean.BaseParse;
import com.gensdai.leliang.remoteInterface.URLConfig;
import com.gensdai.leliang.utils.ViewHolder;

import java.util.Map;

/**
 * 订单评论
 * Created by Administrator on 2017/6/9.
 */

public class OrderCommentProductListAdapter extends KimiraAdapter<BaseParse.OrderAttributeMapBean> {

    LayoutInflater inflater;
    Context context;

    private int index = -1;
    public OrderCommentProductListAdapter(Context context) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }


    @Override
    public View extralView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_order_comment_product_list,
                    null);
        }

        SimpleDraweeView icon = ViewHolder.GetChars(convertView, R.id.productIcon);
        TextView productName = ViewHolder.GetChars(convertView, R.id.title);
        TextView productAttr = ViewHolder.GetChars(convertView, R.id.info);
        EditText commentMessage = ViewHolder.GetChars(convertView, R.id.comment_message);
        BaseParse.OrderAttributeMapBean bean = (BaseParse.OrderAttributeMapBean) getItem(position);

        String imguri = "";
        if(!TextUtils.isEmpty(bean.logo)){
            imguri = URLConfig.BASEDOMAIN + bean.logo;
        }
        icon.setImageURI(Uri.parse(imguri));
        productName.setText(bean.productName);
        productAttr.setText(bean.attributeName);



        commentMessage.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View view, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = position;
                }
                return false;
            }
        });

        commentMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果该edittext有默认内容，还要在if那里进行过滤
                if (index>=0 && index == position ) {
                    ((BaseParse.OrderAttributeMapBean) getItem(position)).commentStr = s.toString();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return convertView;
    }
}
